* May 9, 2025
* Dependencies: Requires rsource.ado, which invokes R to run FactorAnalysis.R, 
* in order to compute factor scores using the same R package RPP used.
* Thus, to run this file, user must install R and the psych package. (We used
* R version 4.4.1 and psych package version psych_2.4.12). User must also 
* provide the correct path to R executable on on their machine.
version 19.5

*********************************************************
* Load and Prep Dataset With All Ethnocentrism Measures *
*********************************************************

* On 8/14/2024, RPP provided qualtrics_raw_data.csv, containing ethnocentrism
* variables that were missing from replication materials they had posted on 
* Harvard Dataverse. We removed extraneous row headers, resulting in 
* qualtrics_raw_data_noheaders.csv, which is loaded below.
* RPP further indicated they used only "good completes". We therefore subset
* to gc == 1.

import delimited using ../data/qualtrics_raw_data_noheaders.csv, varnames(1) clear
rename *, lower
keep if gc == 1 // per RPP email, keep only "good completes"
keep fl_7_do fl_39_do support_nukes ethno*
gen id = _n // File from RPP did not contain unique respondent ID. Create one.

*******************************
* Create Treatment Indicators *
*******************************

* In the provided file, treatments were encoded in fl_7 and fl_39
gen tmt = fl_7_do  + fl_39_do // concatenate RPP treatment indicators
tab tmt

* Independent variable: democracy treatment
gen byte t_democ = 1
replace t_democ = 0 if strpos(tmt, "nondem") ~= 0
label var t_democ "Country Regime"
label define t_democ 0 "Country is not a Democracy" 1 "Country is a Democracy"
label values t_democ t_democ

* Independent variable: race treatment
gen byte t_race = 1 // white
replace t_race = 0 if strpos(tmt, "only") ~= 0
replace t_race = 2 if strpos(tmt, "nonwhite") ~= 0 
label var t_race "Country Race"
label define t_race 0 "Unspecified" 1 "Country Race is White" 2 "Country Race is Nonwhite"
label values t_race t_race
label var t_race "Race of Target"
tab t_race t_democ

* Dependent variable: support for strike
gen byte strike = .
replace strike = 1 if support_nukes == "Oppose strongly"
replace strike = 2 if support_nukes == "Oppose"
replace strike = 3 if support_nukes == "Oppose somewhat"
replace strike = 4 if support_nukes == "Neither favor nor oppose"
replace strike = 5 if support_nukes == "Favor somewhat"
replace strike = 6 if support_nukes == "Favor"
replace strike = 7 if support_nukes == "Favor strongly"
tab strike

* Moderator: ethnocentrism (items 1 through 5)
rename ethnocentr3 ethnocent3
rename ethnocentr4 ethnocent4
forvalues i = 1/5 {
	quietly {
		gen byte ethno`i' = .
		replace ethno`i' = 1 if ethnocent`i' == "Strongly disagree"
		replace ethno`i' = 2 if ethnocent`i' == "Disagree"
		replace ethno`i' = 3 if ethnocent`i' == "Somewhat disagree"
		replace ethno`i' = 4 if ethnocent`i' == "Neither agree nor disagree"
		replace ethno`i' = 5 if ethnocent`i' == "Somewhat agree"
		replace ethno`i' = 6 if ethnocent`i' == "Agree"
		replace ethno`i' = 7 if ethnocent`i' == "Strongly agree"				
	}	
}
drop ethnocent?
label var ethno1 "Most other cultures are backward compared with my culture."
label var ethno2 "My culture should be the role model for other cultures."
label var ethno3 "The world would be a much better place if all other ethnic groups modeled themselves on my ethnic group."
label var ethno4 "In general, I prefer doing things with people from my own ethnic group than with people from other ethnic groups."
label var ethno5 "I am not really interested in the customs and values of other countries."

* rename ethnocentrism vars to match the varnames in Dataverse replication file
rename ethno3 ethnox
rename ethno5 ethno3
rename ethnox ethno5

drop tmt fl_7_do  fl_39_do support_nukes
tempfile dta
save `dta'

*************************************************************************
* Using same R package as RPP, compute Factor Scores for ethno 1, 2, 3  *
* (used by RPP) and ethno 4, 5 (not used by RPP). Merge back into Stata *
*************************************************************************

* Export ethno1 through ethno5 as CSV
export delimited id ethno1 ethno2 ethno3 ethno4 ethno5 using ../output/qualtrics_ethno.csv, nolabel replace

* Using same R package as RPP, compute ethnocentrism factor scores for ethno4 and 5
rsource using FactorAnalysis.R, roptions(`"--vanilla"')

* Import the factor scores into Stata, and merge with full data
import delimited using "../output/qualtrics_factorscores.csv", asdouble clear
drop v1

* Ethnocentrism measures based on ethno1, ethno2, ethno3
label var e3f "Ethno 1,2,3: factor scores"
summarize e3f, det
generate byte e3f_hi = cond(e3f > `r(p50)', 1, 0) if e3f ~= .
label var e3f_hi "Ethnocentrism Above Median"
label define e3f_hi 0 "Ethnocentrism Below Median" 1 "Ethnocentrism Above Median"
label values e3f_hi e3f_hi

* Ethnocentrism measures based on ethno4, ethno5
label var e2f "Ethno 4,5: factor scores"
summarize e2f, det
generate byte e2f_hi = cond(e2f > `r(p50)', 1, 0) if e2f ~= .
label var e2f_hi "Ethnocentrism Above Median"
label define e2f_hi 0 "Ethnocentrism Below Median" 1 "Ethnocentrism Above Median"
label values e2f_hi e2f_hi

tempfile fscores
save `fscores'
use `dta', clear
merge 1:1 id using `fscores', nogenerate

***********
* Analyze *
***********

capture log close
log using ../output/Test4.txt, text nomsg replace

* Table 6: Effect of Democracy and Race, By Ethnocentrism *
regress strike t_democ##e3f_hi ib2.t_race##e3f_hi if t_race ~= 0, robust // their measure
etable, column(estimates) cstat(_r_b, nformat(%5.2f)) ///
   cstat(_r_se, nformat(%5.2f)) mstat(N) showstars showstarsnote ///
	export(../output/Table6.xlsx, replace)

* Supp 1 and 2.1: Does Ethnocentrism Moderate the Eliminated Effect, using RPP's Measure of Ethnocentrism?
regress strike t_democ##t_race##e3f_hi if inlist(t_race, 0, 1), robust // if race is white
etable, column(estimates) cstat(_r_b, nformat(%5.2f)) cstat(_r_se, nformat(%5.2f)) mstat(N) showstars showstarsnote export(../output/Supp1.xlsx, replace)
regress strike t_democ##t_race##e3f_hi if inlist(t_race, 0, 2), robust // if race is nonwhite
etable, column(estimates) cstat(_r_b, nformat(%5.2f)) cstat(_r_se, nformat(%5.2f)) mstat(N) showstars showstarsnote export(../output/Supp2.1.xlsx, replace)

* Supp 3: Does Ethnocentrism Moderate Effect of Race on Y, using Alt Measure of Ethnocentrism?
regress strike t_democ##e2f_hi ib2.t_race##e2f_hi if t_race ~= 0, robust
etable, column(estimates) cstat(_r_b, nformat(%5.2f)) ///
   cstat(_r_se, nformat(%5.2f)) mstat(N) showstars showstarsnote ///
	export(../output/Supp3.xlsx, replace)

* Supp 4 and 5: Does Ethnocentrism Moderate the Eliminated Effect, using Alt Measure of Ethnocentrism?
regress strike t_democ##t_race##e2f_hi if inlist(t_race, 0, 1), robust // if race is white
etable, column(estimates) cstat(_r_b, nformat(%5.2f)) cstat(_r_se, nformat(%5.2f)) mstat(N) showstars showstarsnote export(../output/Supp4.xlsx, replace)
regress strike t_democ##t_race##e2f_hi if inlist(t_race, 0, 2), robust // if race is nonwhite
etable, column(estimates) cstat(_r_b, nformat(%5.2f)) cstat(_r_se, nformat(%5.2f)) mstat(N) showstars showstarsnote export(../output/Supp5.xlsx, replace)
	
log close
